package com.chenyu.controller;

import com.chenyu.service.IUserLoginLogService;
import com.chenyu.domain.UserLoginLog;
import com.chenyu.query.UserLoginLogQuery;
import com.chenyu.util.AjaxResult;
import com.chenyu.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.support.PagedListHolder;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.plugins.Page;

import java.util.List;

@RestController
@RequestMapping("/userLoginLog" )
public class UserLoginLogController {
    @Autowired
    public IUserLoginLogService userLoginLogService;


    /**
     * 保存和修改公用的
     * @param userLoginLog 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody UserLoginLog userLoginLog) {
        if ( userLoginLog.getId() != null)
            userLoginLogService.updateById(userLoginLog);
            else
            userLoginLogService.insert(userLoginLog);
        return AjaxResult.success();
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}" )
    public AjaxResult delete(@PathVariable("id" ) Long id) {
            userLoginLogService.deleteById(id);
        return AjaxResult.success();
    }

    //获取用户
    @GetMapping("/{id}" )
    public AjaxResult get(@PathVariable("id" ) Long id) {
        UserLoginLog userLoginLog =userLoginLogService.selectById(id);
        return AjaxResult.success(userLoginLog);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list() {
        List< UserLoginLog> list = userLoginLogService.selectList(null);
        return AjaxResult.success(list);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list" )
    public AjaxResult json(@RequestBody UserLoginLogQuery query) {
        Page<UserLoginLog> page = new Page<UserLoginLog>(query.getPage(), query.getRows());
        page = userLoginLogService.selectPage(page);
        PageList pageList = new PageList<UserLoginLog>(page.getTotal(), page.getRecords());
        return AjaxResult.success(pageList);
    }
}
